/*
 * @Author: Wangtao
 * @Date: 2022-09-05 20:25:42
 * @LastEditors: Wangtao
 * @LastEditTime: 2022-09-23 10:53:20
 */
import { defineStore } from 'pinia'
import { globalModule } from '@/common/store/modules/global'
import { clientSettingModule } from '@/app/call-center/store/modules/client-setting'
import router from '@app/base/router'
import { newDate } from '@/common/utils/common'
export const callCardModule = defineStore('callCard', {
  state: () => {
    return {
      list: [],
      localGetState: false,
      // 全局通话卡片调用
      globalCardInfo: {
        show: false,
        callId: ''
      }
    }
  },
  actions: {
    async oprateInfoByCallStateInfo(stateInfo: any) {
      // 通过推送状态更新卡片信息
      this.getCardListByLocal()
      if (stateInfo.superUniqueId) {
        // 定位卡片是否存在
        let existCard = false
        let existIndex = 0
        let currentInfo: any = null
        this.list.some((item: any, index: number) => {
          if (stateInfo.superUniqueId === item.superUniqueId) {
            existCard = true
            existIndex = index
            currentInfo = item
            return true
          }
        })
        if (existCard) {
          // 更新卡片
          if (stateInfo.callEvent === 'hangup') {
            currentInfo.isconnected ? (stateInfo.callEvent = 'Answered') : (stateInfo.callEvent = 'noAnswered')
          }
          const setInfo = {
            customerPhone: currentInfo.number || stateInfo.number || '',
            callType: stateInfo.callType || '',
            callEvent: stateInfo.callEvent || '',
            isconnected: currentInfo.callEvent === 'link' ? true : currentInfo.isconnected,
            customerCountry: currentInfo.customerCountry || stateInfo.customerCountry || '',
            customerProvince: currentInfo.customerProvince || stateInfo.customerProvince || ''
          }
          ;(this as any).list[existIndex] = { ...currentInfo, ...setInfo }
          await window.sessionStorage.setItem('cardlist', JSON.stringify(this.list))
        } else {
          if (this.getCardLimitStatus(stateInfo)) {
            return
          }
          // 新增卡片
          const setInfo: any = {
            customerPhone: stateInfo.number || '',
            callType: stateInfo.callType || '',
            callEvent: stateInfo.callEvent || '',
            customerCountry: stateInfo.customerCountry || '',
            customerProvince: stateInfo.customerProvince || '',
            superUniqueId: stateInfo.superUniqueId,
            statusTime: newDate().getTime()
          }
          if (this.list.length >= 20) {
            ;(this as any).list.pop()
          }
          ;(this as any).list.unshift(setInfo)
          await window.sessionStorage.setItem('cardlist', JSON.stringify(this.list))
          const routePath = `/app/card_info/${stateInfo.superUniqueId}`
          await globalModule().notify({
            title: '通知',
            message: '您有新电话卡片',
            path: routePath
          })
          // 自动跳到对应的卡片
          setTimeout(() => {
            if (clientSettingModule().frontCallConfig.autoGo) {
              router.push({ path: routePath })
            }
          }, 500)
        }
      }
    },
    getCardLimitStatus (stateInfo: any) {
      // 咨询不弹屏
      if (stateInfo.callType === 'consult') {
        return true
      } else {
        return false
      }
    },
    async getCardListByLocal() {
      if (this.localGetState) {
        return
      }
      // eslint-disable-next-line no-async-promise-executor
      return new Promise<void>(async (resolve) => {
        const currentlist: any = await window.sessionStorage.getItem('cardlist')
        if (currentlist) {
          this.list = JSON.parse(currentlist)
        }
        this.localGetState = true
        resolve()
      })
    },
    async getCurrentCardInfoById(id: string) {
      let currentCardInfo: any = null
      let returnIndex = 0
      this.list.some((item: any, index: number) => {
        if (id === item.superUniqueId) {
          currentCardInfo = item
          returnIndex = index
          return true
        }
      })
      currentCardInfo.index = returnIndex
      return currentCardInfo
    },
    async updateCardInfo(callId: any, type: any, value: any) {
      let existIndex = 0
      let existCard = false
      this.list.some((item: any, index: number) => {
        if (callId === item.superUniqueId) {
          existIndex = index
          existCard = true
          return true
        }
      })
      if (existCard) {
        console.log(existIndex, type, value)
        ;(this as any).list[existIndex][type] = value
        await window.sessionStorage.setItem('cardlist', JSON.stringify(this.list))
      }
    },
    updateGlobalCardInfo(type: any, value: any) {
      ;(this.globalCardInfo as any)[type] = value
    }
  }
})
